package com.test.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

public class PubsubClientExample {

    private final Jedis jedis;

    public PubsubClientExample(Jedis jedis) {
        this.jedis = jedis;
    }

    public void subscribe(String channel) {
        jedis.subscribe(new JedisPubSub() {
            @Override
            public void onMessage(String channel, String message) {
                // ％n 提供所运行平台的行尾
                System.out.printf("channel:[%s], msg=%s%n", channel, message);
            }

            @Override
            public void punsubscribe() {
                // 取消订阅
                super.punsubscribe();
            }
        }, channel);
    }
}
